package logic

import (
	"context"
	"errors"
	"lm/models/mysql"

	"lm/lmrpc/internal/svc"
	"lm/lmrpc/lmrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LmAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLmAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LmAddLogic {
	return &LmAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LmAddLogic) LmAdd(in *lmrpc.LmAddRequest) (*lmrpc.LmAddResponse, error) {
	// todo: add your logic here and delete this line
	lm := mysql.Lm{
		Title:   in.Title,
		Image:   in.Image,
		Text:    in.Text,
		Price:   float64(in.Price),
		ClassId: int(in.ClassId),
		BrandId: int(in.BrandId),
	}
	mysql.DB.Debug().Create(&lm)
	if in.ClassId == 0 {
		return nil, errors.New("不允许添加")
	}

	return &lmrpc.LmAddResponse{
		LmId: int64(lm.ID),
	}, nil
}
